package com.eastfair.home.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.echo.core.EchoService;
import com.eastfair.home.dto.ReportBuilderAuditPageQuery;
import com.eastfair.home.dto.ReportBuilderAuditSaveDTO;
import com.eastfair.home.dto.ReportBuilderAuditUpdateDTO;
import com.eastfair.home.entity.ReportBuilderAudit;
import com.eastfair.home.service.ReportBuilderAuditService;
import com.eastfair.home.vo.ReportBuilderAuditVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 报馆搭建商信息审核记录
 * </p>
 *
 * @author dq
 * @date 2022-08-08
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/reportBuilderAudit")
@Api(value = "ReportBuilderAudit", tags = "报馆搭建商信息审核记录")
@PreAuth(replace = "home:reportBuilderAudit:")
public class ReportBuilderAuditController extends SuperController<ReportBuilderAuditService, Long, ReportBuilderAudit, ReportBuilderAuditPageQuery, ReportBuilderAuditSaveDTO, ReportBuilderAuditUpdateDTO> {

    @Resource
    private EchoService echoService;

    @Resource
    private ReportBuilderAuditService reportBuilderAuditService;

    @Override
    public void handlerResult(IPage<ReportBuilderAudit> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<ReportBuilderAudit> reportBuilderAuditList = list
                .stream()
                .map((map) -> ReportBuilderAudit.builder().build())
                .collect(Collectors.toList());
        return R.success(baseService.saveBatch(reportBuilderAuditList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<ReportBuilderAuditPageQuery> params) {
        ReportBuilderAuditPageQuery pageQuery = params.getModel();
        //分页处理
        if (ContextUtil.getProjectId() != null) {
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    /**
     * 报馆审核ID查询报馆搭建商审核VO.
     *
     * @param reportSpaceAuditId the report space audit id
     * @return the by report space audit id
     */
    @GetMapping("/reportSpaceAudit/{reportSpaceAuditId}")
    public R<ReportBuilderAuditVO> getByReportSpaceAuditId(@PathVariable("reportSpaceAuditId") Long reportSpaceAuditId) {
        log.info("getByReportSpaceAuditId - 报馆审核ID查询报馆搭建商审核VO, reportSpaceAuditId={}", reportSpaceAuditId);
        return R.success(reportBuilderAuditService.getVOByReportSpaceAuditId(reportSpaceAuditId));
    }

}
